﻿namespace System.Workflow.ComponentModel.Serialization
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;

    internal class CollectionMarkupSerializer : WorkflowMarkupSerializer
    {
        protected internal override void AddChild(WorkflowMarkupSerializationManager serializationManager, object parentObj, object childObj)
        {
            if (parentObj == null)
            {
                throw new ArgumentNullException("parentObj");
            }
            if (!IsValidCollectionType(parentObj.GetType()))
            {
                throw new Exception(SR.GetString("Error_SerializerTypeRequirement", new object[] { parentObj.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName }));
            }
            parentObj.GetType().InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, parentObj, new object[] { childObj }, CultureInfo.InvariantCulture);
        }

        protected internal override void ClearChildren(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (!IsValidCollectionType(obj.GetType()))
            {
                throw new Exception(SR.GetString("Error_SerializerTypeRequirement", new object[] { obj.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName }));
            }
            if (!(obj is ICollection))
            {
                obj.GetType().InvokeMember("Clear", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[0], CultureInfo.InvariantCulture);
            }
        }

        protected internal override IList GetChildren(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (!IsValidCollectionType(obj.GetType()))
            {
                throw new Exception(SR.GetString("Error_SerializerTypeRequirement", new object[] { obj.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName }));
            }
            IEnumerable enumerable = obj as IEnumerable;
            ArrayList list = new ArrayList();
            foreach (object obj2 in enumerable)
            {
                list.Add(obj2);
            }
            return list;
        }

        protected internal override PropertyInfo[] GetProperties(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            return new PropertyInfo[0];
        }

        internal static bool IsValidCollectionType(Type collectionType)
        {
            if (collectionType == null)
            {
                return false;
            }
            if (typeof(Array).IsAssignableFrom(collectionType))
            {
                return false;
            }
            if (!typeof(ICollection).IsAssignableFrom(collectionType))
            {
                if (!collectionType.IsGenericType)
                {
                    return false;
                }
                if (!typeof(ICollection<>).IsAssignableFrom(collectionType.GetGenericTypeDefinition()))
                {
                    return typeof(IList<>).IsAssignableFrom(collectionType.GetGenericTypeDefinition());
                }
            }
            return true;
        }

        protected internal override bool ShouldSerializeValue(WorkflowMarkupSerializationManager serializationManager, object value)
        {
            if (value != null)
            {
                if (!IsValidCollectionType(value.GetType()))
                {
                    throw new Exception(SR.GetString("Error_SerializerTypeRequirement", new object[] { value.GetType().FullName, typeof(ICollection).FullName, typeof(ICollection<>).FullName }));
                }
                IEnumerable enumerable = value as IEnumerable;
                using (IEnumerator enumerator = enumerable.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.Current;
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

